/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArticulosUI.java
 *
 * Created on 6/01/2012, 11:13:59 AM
 */
package gui.panel;

import dao.ArticuloDao;
import dao.CategoriaDao;
import dao.IvaDao;
import dao.MarcaDao;
import dao.ReferenciaDao;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import pojos.Articulo;
import pojos.Categoria;
import pojos.Iva;
import pojos.Marca;
import pojos.Referencia;
import util.LoggingBindingListener;

/**
 *
 * @author Usuario
 */
public class ArticulosUI extends javax.swing.JPanel {

    
    //beans y elementos para mostrar en pantalla
    private List<Marca> listMarcas= new ArrayList<Marca>();
    private List<Categoria> listCats = new ArrayList<Categoria>();
    public static final String PROP_CATS = "listCats";

    private List<Referencia> listRefs = new ArrayList<Referencia>();
    private List<Iva> listIva = new ArrayList<Iva>();
    private Articulo articulo = new Articulo();
    
    //daos
    private ArticuloDao artDao=new ArticuloDao();
    private CategoriaDao catDao=new CategoriaDao();
    private ReferenciaDao refDao=new ReferenciaDao();
    private MarcaDao marcaDao=new MarcaDao();
    private IvaDao ivaDao=new IvaDao();
    
    //variable aux
    private SpinnerModel modeloSpinner;
    /** Creates new form ArticulosUI */
    public ArticulosUI(Articulo artic) {

        listMarcas=marcaDao.findAll();
        listCats=catDao.findAll();
        listRefs=refDao.findAll();
        listIva=ivaDao.findAll();
        if(artic != null)
            this.articulo=artic;

        initComponents();
        
        //cambio del comportamiento por defecto de los componentes
        AutoCompleteDecorator.decorate(comboCateg);
        AutoCompleteDecorator.decorate(comboMarca);
        AutoCompleteDecorator.decorate(comboMod);
        modeloSpinner=new SpinnerNumberModel(0.25, 0, 1, 0.01);
        spUtil.setModel(modeloSpinner);

        //Etiqueta para mostrar los errores
        bindingGroup.addBindingListener(new LoggingBindingListener(msgLabel));

        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        requiredStringValidator1 = new validation.RequiredStringValidator();
        requiredNumbersValidator1 = new validation.RequiredNumbersValidator();
        bigDecimalSpinnerConverter1 = new validation.converter.BigDecimalSpinnerConverter();
        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();
        lDescripcion = new javax.swing.JLabel();
        textDescrip = new javax.swing.JTextField();
        lUnidad = new javax.swing.JLabel();
        textUnidad = new javax.swing.JTextField();
        lIva = new javax.swing.JLabel();
        comboIva = new javax.swing.JComboBox();
        lCategoria = new javax.swing.JLabel();
        lMarca = new javax.swing.JLabel();
        lModel = new javax.swing.JLabel();
        comboCateg = new javax.swing.JComboBox();
        comboMarca = new javax.swing.JComboBox();
        comboMod = new javax.swing.JComboBox();
        bAgregarMarca = new javax.swing.JButton();
        bEditMarca = new javax.swing.JButton();
        bAgregCat = new javax.swing.JButton();
        bEditCat = new javax.swing.JButton();
        bAgregMod = new javax.swing.JButton();
        bEditMod = new javax.swing.JButton();
        lColor = new javax.swing.JLabel();
        ltam = new javax.swing.JLabel();
        textColor = new javax.swing.JTextField();
        textTam = new javax.swing.JTextField();
        lMargen = new javax.swing.JLabel();
        spUtil = new javax.swing.JSpinner();
        msgLabel = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jXTitledSeparator2 = new org.jdesktop.swingx.JXTitledSeparator();
        lColor1 = new javax.swing.JLabel();
        textCU = new javax.swing.JTextField();
        textTam2 = new javax.swing.JTextField();
        lColor2 = new javax.swing.JLabel();
        lColor3 = new javax.swing.JLabel();
        textCT = new javax.swing.JTextField();

        jXTitledSeparator1.setFont(new java.awt.Font("Helvetica Neue", 1, 18));
        jXTitledSeparator1.setTitle("Artículos y servicios");

        lDescripcion.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lDescripcion.setText("Descripción*:");

        textDescrip.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.descripcionArt}"), textDescrip, org.jdesktop.beansbinding.BeanProperty.create("text"), "Descripción");
        binding.setValidator(requiredStringValidator1);
        bindingGroup.addBinding(binding);

        textDescrip.addKeyListener(new validation.KeyLimiter(50, textDescrip));

        lUnidad.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lUnidad.setText("Unidad de medida*:");

        textUnidad.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.unidadMedida}"), textUnidad, org.jdesktop.beansbinding.BeanProperty.create("text"), "Unidad de medida");
        bindingGroup.addBinding(binding);

        textUnidad.addKeyListener(new validation.KeyLimiter(10, textUnidad));

        lIva.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lIva.setText("IVA*:");

        comboIva.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listIvas}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, comboIva);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.iva}"), comboIva, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        comboIva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboIvaActionPerformed(evt);
            }
        });

        lCategoria.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lCategoria.setText("Categoria*:");

        lMarca.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lMarca.setText("Marca*:");

        lModel.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lModel.setText("Modelo:");

        comboCateg.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listCats}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, comboCateg);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.categoria}"), comboCateg, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        comboMarca.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listMarcas}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, comboMarca);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.marca}"), comboMarca, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        comboMod.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listRefs}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, comboMod);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.referencia}"), comboMod, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        bAgregarMarca.setText("+");
        bAgregarMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAgregarMarcaActionPerformed(evt);
            }
        });

        bEditMarca.setText("E");
        bEditMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditMarcaActionPerformed(evt);
            }
        });

        bAgregCat.setText("+");
        bAgregCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAgregCatActionPerformed(evt);
            }
        });

        bEditCat.setText("E");
        bEditCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditCatActionPerformed(evt);
            }
        });

        bAgregMod.setText("+");
        bAgregMod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAgregModActionPerformed(evt);
            }
        });

        bEditMod.setText("E");
        bEditMod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditModActionPerformed(evt);
            }
        });

        lColor.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lColor.setText("Color:");

        ltam.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        ltam.setText("Tamaño:");

        textColor.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.color}"), textColor, org.jdesktop.beansbinding.BeanProperty.create("text"), "Color");
        bindingGroup.addBinding(binding);

        textColor.addKeyListener(new validation.KeyLimiter(20, textColor));

        textTam.setFont(new java.awt.Font("Helvetica Neue", 0, 14));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.tamanyo}"), textTam, org.jdesktop.beansbinding.BeanProperty.create("text"), "Tamaño");
        bindingGroup.addBinding(binding);

        textTam.addKeyListener(new validation.KeyLimiter(20, textTam));

        lMargen.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lMargen.setText("Margen utilidad*:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.margenUtilidad}"), spUtil, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setConverter(bigDecimalSpinnerConverter1);
        bindingGroup.addBinding(binding);

        msgLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        msgLabel.setForeground(new java.awt.Color(204, 0, 0));

        jButton7.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton7.setText("Guardar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton8.setText("Cancelar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton9.setText("C");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jXTitledSeparator2.setFont(new java.awt.Font("Helvetica Neue", 1, 18));
        jXTitledSeparator2.setTitle("Valorización en inventarios");

        lColor1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lColor1.setText("Costo unidad:");

        textCU.setEditable(false);
        textCU.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        textCU.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.saldoVu}"), textCU, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        textTam2.setEditable(false);
        textTam2.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        textTam2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.saldoCant}"), textTam2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        lColor2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lColor2.setText("Existencias:");

        lColor3.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14));
        lColor3.setText("Total costo:");

        textCT.setEditable(false);
        textCT.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        textCT.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${articulo.saldoVt}"), textCT, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lMargen)
                    .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lIva)
                        .addGap(363, 363, 363))
                    .addComponent(lModel)
                    .addComponent(lMarca)
                    .addComponent(lCategoria)
                    .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(msgLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton9))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lColor1)
                        .addGap(20, 20, 20)
                        .addComponent(textCU, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(lColor2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textTam2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lColor3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCT, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(ltam)
                                    .addGap(273, 273, 273))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(lColor)
                                    .addGap(290, 290, 290)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lDescripcion)
                                    .addComponent(lUnidad))
                                .addGap(23, 23, 23)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(comboCateg, javax.swing.GroupLayout.Alignment.LEADING, 0, 175, Short.MAX_VALUE)
                                    .addComponent(comboMarca, javax.swing.GroupLayout.Alignment.LEADING, 0, 175, Short.MAX_VALUE)
                                    .addComponent(comboMod, 0, 175, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(bAgregMod)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bEditMod))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(bAgregCat)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bEditCat))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(bAgregarMarca)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bEditMarca)))
                                .addGap(4, 4, 4))
                            .addComponent(textDescrip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addComponent(textTam, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addComponent(spUtil, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addComponent(textColor, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addComponent(comboIva, javax.swing.GroupLayout.Alignment.LEADING, 0, 271, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textUnidad, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lDescripcion)
                    .addComponent(textDescrip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lUnidad)
                    .addComponent(textUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lIva)
                    .addComponent(comboIva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lCategoria)
                        .addComponent(comboCateg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bAgregarMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(bEditMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lMarca)
                        .addComponent(comboMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bAgregCat, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(bEditCat, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lModel)
                        .addComponent(comboMod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bAgregMod, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(bEditMod, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lColor)
                    .addComponent(textColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ltam)
                    .addComponent(textTam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lMargen)
                    .addComponent(spUtil, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lColor1)
                    .addComponent(textTam2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lColor2)
                    .addComponent(lColor3)
                    .addComponent(textCT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(msgLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void comboIvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboIvaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboIvaActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:       
        try {
            artDao.create(articulo);
            msgLabel.setText("La información se ha guardado correctamente");
        } catch (RuntimeException e) {
            msgLabel.setText("Error: " + e.getMessage());
            //throw e; //en caso de requerir debug
        }

    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        //limpiar todos los campos
        this.setArticulo( new Articulo());
        
    }//GEN-LAST:event_jButton9ActionPerformed

    private void bAgregarMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAgregarMarcaActionPerformed
        // TODO add your handling code here:
        //establecer el limite del campo en la base de datos
        
        int lim=50;
        String cadena = JOptionPane.showInputDialog(this, 
                "Escriba el nombre de la nueva categoria");
        //hasta que encuentre una forma mas sencilla
        if (cadena != null && ! "".equals(cadena)) {
            String result = cadena;
            if (cadena.length() > lim) {
                result = cadena.substring(0, lim);
            }

            try {
                //verificamos que no hayan duplicados
                Categoria existe=catDao.findMatch(result);
                if(existe==null){
                    catDao.create(new Categoria(result));
                    this.setListCats(catDao.findAll());
                    existe = catDao.findByDescrip(result).get(0);
                }
                articulo.setCategoria(existe);
            } catch (Exception e) {
                msgLabel.setText(e.getMessage());
            }
        }
    }//GEN-LAST:event_bAgregarMarcaActionPerformed

    private void bEditMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditMarcaActionPerformed
        // TODO add your handling code here:
        int lim=50;
        Categoria edit=(Categoria)comboCateg.getSelectedItem();
        if(edit!=null){
            String cadena = JOptionPane.showInputDialog(this,
                    "Escriba el nuevo nombre de la categoría", edit.getDescripcionCat());
            if (cadena != null && !"".equals(cadena)) {
                edit.setDescripcionCat(cadena);
                if (cadena.length() > lim) {
                    edit.setDescripcionCat(cadena.substring(0, lim));
                }
                try {
                    catDao.create(edit);
                    this.setListCats(catDao.findAll());
                    comboCateg.setSelectedItem(edit);
                    comboCateg.updateUI();

                } catch (Exception e) {
                    msgLabel.setText(e.getMessage());
                }
            }
        }
    }//GEN-LAST:event_bEditMarcaActionPerformed

    private void bAgregCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAgregCatActionPerformed
        // TODO add your handling code here:
        //metodo repetido, conviene abstraerlo en un metodo generico
        int lim = 30;
        String cadena = JOptionPane.showInputDialog(this,
                "Escriba el nombre de la nueva Marca");
        //hasta que encuentre una forma mas sencilla
        if (cadena != null && !"".equals(cadena)) {
            String result = cadena;
            if (cadena.length() > lim) {
                result = cadena.substring(0, lim);
            }

            try {
                Marca existe = marcaDao.findMatch(result);
                if (existe == null) {
                    marcaDao.create(new Marca(result));
                    this.setListMarcas(marcaDao.findAll());
                    existe = marcaDao.findByDescrip(result).get(0);
                }
                articulo.setMarca(existe);
            } catch (Exception e) {
                msgLabel.setText(e.getMessage());
            }
        }
    }//GEN-LAST:event_bAgregCatActionPerformed

    private void bEditCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditCatActionPerformed
        // TODO add your handling code here:
        //metodo repetido
        int lim = 30;
        Marca edit = (Marca) comboMarca.getSelectedItem();
        if(edit != null){
            String cadena = JOptionPane.showInputDialog(this,
                    "Escriba el nuevo nombre de la Marca", edit.getDescripcionMarca());
            if (cadena != null && !"".equals(cadena)) {
                edit.setDescripcionMarca(cadena);
                if (cadena.length() > lim) {
                    edit.setDescripcionMarca(cadena.substring(0, lim));
                }
                try {
                    marcaDao.create(edit);
                    this.setListMarcas(marcaDao.findAll());
                    comboMarca.setSelectedItem(edit);
                    comboMarca.updateUI();

                } catch (Exception e) {
                    msgLabel.setText(e.getMessage());
                }
            }
        }
    }//GEN-LAST:event_bEditCatActionPerformed

    private void bAgregModActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAgregModActionPerformed
        // TODO add your handling code here:
        //metodo repetido, conviene abstraerlo en un metodo generico
        int lim = 30;
        String cadena = JOptionPane.showInputDialog(this,
                "Escriba el nombre del nuevo modelo");
        //hasta que encuentre una forma mas sencilla
        if (cadena != null && !"".equals(cadena)) {
            String result = cadena;
            if (cadena.length() > lim) {
                result = cadena.substring(0, lim);
            }

            try {
                Referencia existe = refDao.findMatch(result);
                if (existe == null) {
                    refDao.create(new Referencia(result));
                    this.setListRefs(refDao.findAll());
                    existe = refDao.findByDescrip(result).get(0);
                }
                articulo.setReferencia(existe);
            } catch (Exception e) {
                msgLabel.setText(e.getMessage());
            }
        }
    }//GEN-LAST:event_bAgregModActionPerformed

    private void bEditModActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditModActionPerformed
        // TODO add your handling code here:
        //metodo repetido
        int lim = 30;
        Referencia edit = (Referencia) comboMod.getSelectedItem();

        if(edit != null){
            String cadena = JOptionPane.showInputDialog(this,
                    "Escriba el nuevo nombre del nuevo modelo", edit.getDescripcionRef());
            if (cadena != null && !"".equals(cadena)) {
                edit.setDescripcionRef(cadena);
                if (cadena.length() > lim) {
                    edit.setDescripcionRef(cadena.substring(0, lim));
                }
                try {
                    refDao.create(edit);
                    this.setListRefs(refDao.findAll());
                    comboMod.setSelectedItem(edit);
                    comboMod.updateUI();

                } catch (Exception e) {
                    msgLabel.setText(e.getMessage());
                }
            }
        }
    }//GEN-LAST:event_bEditModActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAgregCat;
    private javax.swing.JButton bAgregMod;
    private javax.swing.JButton bAgregarMarca;
    private javax.swing.JButton bEditCat;
    private javax.swing.JButton bEditMarca;
    private javax.swing.JButton bEditMod;
    private validation.converter.BigDecimalSpinnerConverter bigDecimalSpinnerConverter1;
    private javax.swing.JComboBox comboCateg;
    private javax.swing.JComboBox comboIva;
    private javax.swing.JComboBox comboMarca;
    private javax.swing.JComboBox comboMod;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator2;
    private javax.swing.JLabel lCategoria;
    private javax.swing.JLabel lColor;
    private javax.swing.JLabel lColor1;
    private javax.swing.JLabel lColor2;
    private javax.swing.JLabel lColor3;
    private javax.swing.JLabel lDescripcion;
    private javax.swing.JLabel lIva;
    private javax.swing.JLabel lMarca;
    private javax.swing.JLabel lMargen;
    private javax.swing.JLabel lModel;
    private javax.swing.JLabel lUnidad;
    private javax.swing.JLabel ltam;
    private javax.swing.JLabel msgLabel;
    private validation.RequiredNumbersValidator requiredNumbersValidator1;
    private validation.RequiredStringValidator requiredStringValidator1;
    private javax.swing.JSpinner spUtil;
    private javax.swing.JTextField textCT;
    private javax.swing.JTextField textCU;
    private javax.swing.JTextField textColor;
    private javax.swing.JTextField textDescrip;
    private javax.swing.JTextField textTam;
    private javax.swing.JTextField textTam2;
    private javax.swing.JTextField textUnidad;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public List<Marca> getListMarcas() {
        return listMarcas;
    }

    public List<Categoria> getListCats() {
        return listCats;
    }

    public List<Referencia> getListRefs() {
        return listRefs;
    }

    public List<Iva> getListIvas() {
        return listIva;
    }

    public Articulo getArticulo() {
        return articulo;
    }

    public void setListMarcas(List<Marca> listMarcas) {
        List<Marca> old=this.listMarcas;
        this.listMarcas = listMarcas;
        firePropertyChange("listMarcas", old, listMarcas);
    }

    public void setListCats(List<Categoria> listCats) {
        List<Categoria> old=this.listCats;
        this.listCats = listCats;
        firePropertyChange(PROP_CATS, old, listCats);
    }

    public void setListRefs(List<Referencia> listRefs) {
        List<Referencia> old=this.listRefs;
        this.listRefs = listRefs;
        firePropertyChange("listRefs", old, listRefs);
    }

    public void setListIvas(List<Iva> listIvas) {
        this.listIva = listIvas;
    }

    public void setArticulo(Articulo articulo) {
        Articulo old=this.articulo;
        this.articulo = articulo;
        firePropertyChange("articulo", old, articulo);
    }
    
}
